<!-- src/views/NewsDetail.vue -->
<template>
  <div class="news-detail-container">
    <TopBar />
    <div class="content">
      <MenuLine />
      <div class="news-detail">
        <el-card v-if="news" class="news-card">
          <h1>{{ news.title }}</h1>
          <div class="news-meta">
            <span>发布时间: {{ formatDate(news.createTime) }}</span>
          </div>
          <img
            v-if="news.coverUrl"
            :src="news.coverUrl"
            alt="新闻封面"
            class="news-image"
            @error="handleImageError"
          />
          <div class="news-content" v-html="news.content"></div>
          <div class="actions">
            <el-button type="primary" @click="$router.push('/news')">返回新闻列表</el-button>
          </div>
        </el-card>
        <el-empty v-else description="新闻不存在或正在加载..." />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useNewsStore } from '@/stores/news';
import TopBar from '@/components/TopBar.vue';
import MenuLine from '@/components/MenuLine.vue';

const route = useRoute();
const router = useRouter();
const newsStore = useNewsStore();

const news = ref(null);
const defaultImage = 'https://picsum.photos/400/200?random=0';

const handleImageError = (e: Event) => {
  (e.target as HTMLImageElement).src = defaultImage;
};

const formatDate = (date: string) => {
  return new Date(date).toLocaleString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
};

onMounted(async () => {
  try {
    const newsId = Number(route.params.id);
    if (isNaN(newsId)) {
      throw new Error('无效的新闻ID');
    }
    const newsData = await newsStore.fetchNewsById(newsId);
    if (!newsData) {
      throw new Error('新闻不存在');
    }
    news.value = newsData;
  } catch (error: any) {
    ElMessage.error('加载新闻失败: ' + error.message);
    news.value = null;
  }
});
</script>

<style scoped>
.news-detail-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding-bottom: 60px;
}

.content {
  padding-top: 60px;
  width: 80%;
  margin: 0 auto;
}

.news-detail {
  margin-top: 20px;
}

.news-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.news-card h1 {
  font-size: 28px;
  color: #333;
  margin-bottom: 20px;
  text-align: center;
}

.news-meta {
  font-size: 14px;
  color: #999;
  margin-bottom: 20px;
  text-align: center;
}

.news-image {
  display: block;
  max-width: 800px; /* 新增：限制最大宽度 */
  max-height: 300px; /* 调整：减小最大高度 */
  width: 100%; /* 保持响应式 */
  height: auto; /* 确保高度自适应 */
  margin: 0 auto 20px; /* 居中并保留底部间距 */
  object-fit: cover;
  border-radius: 4px;
}

.news-content {
  font-size: 16px;
  color: #333;
  line-height: 1.8;
  margin-bottom: 20px;
}

.news-content >>> p {
  margin: 10px 0;
}

.actions {
  text-align: center;
}
</style>